{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0633ddc6-5c65-4ca6-8fb2-899c622422ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Amistad (1997)': 2.0,\n",
       " 'L.A. Confidential (1997)': 3.0,\n",
       " 'Mother (1996)': 1.0,\n",
       " 'Money Talks (1997)': 5.0,\n",
       " 'In & Out (1997)': 4.0,\n",
       " 'Apt Pupil (1998)': 4.0,\n",
       " 'Air Bud (1997)': 5.0,\n",
       " 'Seven Years in Tibet (1997)': 4.0,\n",
       " 'Ma vie en rose (My Life in Pink) (1997)': 5.0,\n",
       " 'Life Less Ordinary, A (1997)': 5.0,\n",
       " 'FairyTale: A True Story (1997)': 4.0,\n",
       " 'Wings of the Dove, The (1997)': 5.0,\n",
       " 'Air Force One (1997)': 3.0,\n",
       " 'Letter From Death Row, A (1998)': 5.0,\n",
       " 'Everyone Says I Love You (1996)': 3.0,\n",
       " 'Titanic (1997)': 3.0,\n",
       " 'Soul Food (1997)': 3.0,\n",
       " 'Postman, The (1997)': 4.0,\n",
       " 'Wedding Singer, The (1998)': 5.0,\n",
       " 'English Patient, The (1996)': 5.0,\n",
       " 'G.I. Jane (1997)': 5.0}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import recommendations\n",
    "prefs = recommendations.loadMovieLens('../data/movielens')\n",
    "prefs['88']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9896f3b3",
   "metadata": {},
   "source": [
    "获取基于用户的推荐"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "249a0019-bf04-4934-b05d-d0368a5d22ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(5.0, 'They Made Me a Criminal (1939)'),\n",
       " (5.0, 'Star Kid (1997)'),\n",
       " (5.0, 'Santa with Muscles (1996)'),\n",
       " (5.0, 'Saint of Fort Washington, The (1993)'),\n",
       " (5.0, 'Marlene Dietrich: Shadow and Light (1996) '),\n",
       " (5.0, 'Great Day in Harlem, A (1994)'),\n",
       " (5.0, 'Entertaining Angels: The Dorothy Day Story (1996)'),\n",
       " (5.0, 'Boys, Les (1997)'),\n",
       " (4.89884443128923, 'Legal Deceit (1997)'),\n",
       " (4.815019082242709, 'Letter From Death Row, A (1998)'),\n",
       " (4.7321082983941425, 'Hearts and Minds (1996)'),\n",
       " (4.696244466490867, 'Pather Panchali (1955)'),\n",
       " (4.652397061026758, 'Lamerica (1994)'),\n",
       " (4.538723693474813, 'Leading Man, The (1996)'),\n",
       " (4.535081339106105, 'Mrs. Dalloway (1997)'),\n",
       " (4.532337612572981, 'Innocents, The (1961)'),\n",
       " (4.527998574747076, 'Casablanca (1942)'),\n",
       " (4.510270149719864, 'Everest (1998)'),\n",
       " (4.493967755428438, 'Dangerous Beauty (1998)'),\n",
       " (4.485151301801341, 'Wallace & Gromit: The Best of Aardman Animation (1996)')]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "recommendations.getRecommendations(prefs, '87')[0:20]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39768118",
   "metadata": {},
   "source": [
    "获取基于电影的推荐\n",
    "计算物品的相似度字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a86b6f83-af10-43ef-a762-884b116d0880",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100 / 1664\n",
      "200 / 1664\n",
      "300 / 1664\n",
      "400 / 1664\n",
      "500 / 1664\n",
      "600 / 1664\n",
      "700 / 1664\n",
      "800 / 1664\n",
      "900 / 1664\n",
      "1000 / 1664\n",
      "1100 / 1664\n",
      "1200 / 1664\n",
      "1300 / 1664\n",
      "1400 / 1664\n",
      "1500 / 1664\n",
      "1600 / 1664\n"
     ]
    }
   ],
   "source": [
    "itemsim = recommendations.calculateSimilarItems(prefs, n=50)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2958fa1",
   "metadata": {},
   "source": [
    "计算推荐物品"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0b493443",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(5.0, \"What's Eating Gilbert Grape (1993)\"),\n",
       " (5.0, 'Vertigo (1958)'),\n",
       " (5.0, 'Usual Suspects, The (1995)'),\n",
       " (5.0, 'Toy Story (1995)'),\n",
       " (5.0, 'Titanic (1997)'),\n",
       " (5.0, 'Sword in the Stone, The (1963)'),\n",
       " (5.0, 'Stand by Me (1986)'),\n",
       " (5.0, 'Sling Blade (1996)'),\n",
       " (5.0, 'Silence of the Lambs, The (1991)'),\n",
       " (5.0, 'Shining, The (1980)'),\n",
       " (5.0, 'Shine (1996)'),\n",
       " (5.0, 'Sense and Sensibility (1995)'),\n",
       " (5.0, 'Scream (1996)'),\n",
       " (5.0, 'Rumble in the Bronx (1995)'),\n",
       " (5.0, 'Rock, The (1996)'),\n",
       " (5.0, 'Robin Hood: Prince of Thieves (1991)'),\n",
       " (5.0, 'Reservoir Dogs (1992)'),\n",
       " (5.0, 'Police Story 4: Project S (Chao ji ji hua) (1993)'),\n",
       " (5.0, 'House of the Spirits, The (1993)'),\n",
       " (5.0, 'Fresh (1994)')]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "recommendations.getRecommendedItems(prefs, itemsim, '87')[0:20]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
